package Demo;

import com.sun.javaws.Main;

public class MutexSolution {
    /*
     * 使用三个线程来模拟多线程的场景
     * 使用3条线程，将一个值，从0累加到100
     * */

    //下面是使用互斥锁——悲观锁的解决方法
    static Integer num = 0;

    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    synchronized (Main.class){
                        while (num < 1000) {
                            System.out.println("Thead Name:" + Thread.currentThread().getName() +" : "+ ++num);
                        }
                    }
                }
            });
            thread.start();
        }
    }
}
